跳至主要内容

Python

Python入門到忘記

print("Hello ! I am Maximilian aka Snackeyes.")

辛苦各位跑完雲科路跑~~

相信不管是跑20km、10km還是5km


還以為今天是藍色妖姬聚會呢~


Python到底是啥? 丕語言? 能養嗎?


可以喔!

如果不介意Coding到一半,作業沒了,人也沒了


簡單介紹一下

創始人-吉多·范羅蘇姆

1956 - 2022 荷蘭人
於昨天仍然活著

- About Python -

於在1991年上市
Python一詞來至BBC電視劇《Monty Python的飛行馬戲團》
為ABC後繼語言,強調程式碼的可讀性和簡潔的語法
優雅。明確。簡單

工欲善其事。必先利其器


首先 Python 3安裝


Python 3.11(2022.10.24 Updated)

Download

記得看清楚陣營下載 -> Windows & macOS(🍎 Apple最讚了!)


# 安裝注意事項

記得安裝時要加入PATH(環境變數)

小建議install for All User


VScode 延伸模組


看到下載就對ㄌ~~


基本語法教學


型別認識&變數

a="123"#str
b=123#int
c=123.456#float
d=True #bool

Python是動態強型別的語言,你把甚麼丟給變數,變數就是甚麼


查看型別

type(a)#str
type(b)#int
type(c)#float
type(d)#bool

容器型別

  1. list []
  2. tuple ()
  3. dictionary {}
  4. set {}

list列表

list(數字、字串、布林值)

list1 = ["apple", "banana", "cherry"]
# 0 1 2
# -3 -2 -1
list2 = [1, 5, 7, 9, 3]
list3 = [True, False, False]
type(list1)#<class 'list'>

list其他用法

print(list2[1:3])#從第1位開始取到第3位但不包含第3位
list1[0]="berry"#修改
list2.append(11)#新增
list2.remove(11)#刪除
list2.insert(2,57)#插入(位子,數字)
list2.sort()#排序小至大
list2.reverse()#大至小
print(list2.index(9))#尋找字在列表中位子的值

tuple元組

不能修改或增加!!

tuple1 = ("apple", "banana", "cherry")
tuple2 = (1, 5, 7, 9, 3)
tuple3 = (True, False, False)

dicionary字典

thisdict = {
"name": "阿扁巴巴大俠",
"age": 19,
"department":"EE"
}
print(thisdict["name"]) #阿扁巴巴大俠


set

跟字典一樣是無序,重複出現算一個
可變集合(set);不可變集合(frozenset)
set1 = set('apppppppple')
print(set1)#{'a', 'l', 'e', 'p'}
set1 = {'MON','TUE','WED','THU','FRI','SAT','SUN'}
print('SEV' in set1)#flase

set 集合運算 & 運算子


蛤?你說啥?りしれ供さ小!


假設有A和B兩個集合,如果A中的每個元素都是B的元素
-> A是B的子集合
-> B是A的超集合
如果A是B的子集,但A不等於B(即B中至少存在一個元素不在A集合中)
-> A是B的真子集合
-> B是A的真超集合

Example

s0 = {1,2,3,4}
s1 = {3,4,5,6,7}
s2 = {1,2,3}
print(s2<=s0)#True
print(s2<s0)#True
print(s0>s2,s0>s0)#True,False
print(s0&s1)#交集{3, 4}
print(s0-s1)#差集{1, 2}

互動功能

ans=input("請輸入您的答案")#注意輸入的型態是字串 EX:helloworld
#input() 括弧裡的東西 是輸入時的提示文字
print(ans)#印出helloworld
print補充
print的輸出方式有很多種 同時可以用逗點或加來輸出不同型別的參數
name ="阿扁巴巴大俠"
print(name +"生日: ",920410,"身高:",173)

還行ㄇ各位~


range

range(stop)預設從0開始

range(start, stop[, step])

range(5)#0-4
range(0,5)#0-4
range(3,5)#3-4
rang(1,10,2)#1, 3, 5, 7, 9
print(list(range(5)))

LOOP(for & while)

for variable in range(9):#in 後面要接迭代物件 比如List tuple dictionary
print(variable) #0 1 2 3 4 5 6 7 8
variable = 8
while variable >0:#while 接 condition
print(variable)# 8 7 6 5 4 3 2 1
variable-=1

if & elif & else

if variale==0:
print("variable == 0")
elif variable ==1:
print("variable == 1")
else:
print("variable not 1 or 0")


define function

def 函式名稱(參數名稱1,參數名稱2):
return 參數名稱1+參數名稱2

函式名稱(123,456)#579

強制轉換型別

int("123")#123
str(123)#"123"
float(123)#123.0

實作環節

創建一個小程式 功能是 使用者輸入N位同學的姓名後再輸入三個科目成績 分別是 國文,英文,數學成績(使用函數)

創建一個function

每科>=60才能拿到畢業證書

有一顆<60 直接21

其中一科 拿90以上 得到獎學金不累計(只判斷有沒有而已)


Input

2 //同學有N位
阿扁巴巴大俠
90
80
70
類比巴巴大俠
50
40
70

Output

阿扁巴巴大俠:獲得畢業證書 有獎學金
類比巴巴大俠:你被21了

太簡單?

額外題目


那就掰掰拉~